#include <iostream>
#include "Windows/WinSystem.hpp"
#include <string>
#include <filesystem>
#include <future>
#include <chrono>
#include <thread>
#include "Windows/WinProcess.hpp"
#include "Windows/SharedMemory.hpp"
#include <list>
#include <TlHelp32.h>
#include "DeamonProcess.hpp"
#include <iostream>
#include "Startup.hpp"
#include "DeamonProcess.hpp"
#include "Windows/Dialog.hpp"
#include "GuardProcess.hpp"
#include "Windows/ProcessReadWriteLock.hpp"
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <type_traits>
#include "Defines.hpp"
#include "Windows/CrossProcessMutex.hpp"

// #define RELEASE

/*
1、等待主进程退出
2、判断是否由主进程主动退出
3、如果是主进程主动退出，关闭保护程序，否则重启主进程
*/

using namespace CppWinApi;

#define ReadWriteLock CppWinApi::Windows::ProcessReadWriteLock<APP_UUID>::Value

struct MyMemory
{
    int A;
};

// auto &mInstance = CppWinApi::Windows::SharedMemory<APP_UUID, MyMemory>::GetInstance();
auto &rwInstance = CppWinApi::Windows::ProcessReadWriteLock<APP_UUID>::GetInstance();

int main(int argc, char **args)
{
    using namespace CppWinApi::Windows;

    auto kkk = std::async([]()
                          {
    CrossProcessMutex mutex("my_mutex");

                   while (true)
                   {
                    mutex.Lock();
                    std::cout << "execute task in " << ::GetCurrentThreadId() << std::endl;
                    std::this_thread::sleep_for(std::chrono::microseconds(2000));
                    mutex.Unlock();
                   } });

    auto kkkkk = std::async([]()
                            {
    CrossProcessMutex mutex("my_mutex");

                   while (true)
                   { 
                    mutex.Lock();
                    std::cout << "execute task in " << ::GetCurrentThreadId() << std::endl;
                      std::this_thread::sleep_for(std::chrono::microseconds(2000));
                    mutex.Unlock();
                   } });

    std::cin.get();

    // std::list<std::shared_future<void>> kk;

    // auto processNumber = std::string(args[1]);

    // if (processNumber == "1")
    // {
    //     std::cout << "process number: 1\n";
    //     CppWinApi::Windows::WinProcess::StartProcess(args[0], CREATE_NEW_CONSOLE, "2");
    //     auto future = std::async(std::launch::async, []()
    //                              {
    //                                  for (;;)
    //                                  {
    //                                      rwInstance.WriteAction([]()
    //                                                             {
    //                        std::cout << "run write task with thread " << ::GetCurrentThreadId() << std::endl;
    //                        std::this_thread::sleep_for(std::chrono::milliseconds(200));
    //                        std::cout << "end write task with thread " << ::GetCurrentThreadId() << std::endl; });
    //                                  } });

    //     kk.push_back(future.share());
    // }

    // if (processNumber == "2")
    // {
    //     std::cout << "process number: 2\n";
    //     CppWinApi::Windows::WinProcess::StartProcess(args[0], CREATE_NEW_CONSOLE, "3");
    //     auto future = std::async(std::launch::async, []()
    //                              {
    //                                     for(;;)
    //                                     {
    //                                         rwInstance.ReadAction([]()
    //                                                          {
    //                        std::cout << "run read task with thread " << ::GetCurrentThreadId() << std::endl;
    //                        std::this_thread::sleep_for(std::chrono::milliseconds(200));
    //                        std::cout << "end read task with thread " << ::GetCurrentThreadId() << std::endl; });
    //                                     } });

    //     kk.push_back(future.share());
    // }

    // if (processNumber == "3")
    // {
    //     std::cout << "process number: 3\n";
    //     auto future = std::async(std::launch::async, []()
    //                              {
    //                                     for(;;)
    //                                     {
    //                                         rwInstance.ReadAction([]()
    //                                                          {
    //                        std::cout << "run read task with thread " << ::GetCurrentThreadId() << std::endl;
    //                        std::this_thread::sleep_for(std::chrono::milliseconds(200));
    //                        std::cout << "end read task with thread " << ::GetCurrentThreadId() << std::endl; });
    //                                     } });

    //     kk.push_back(future.share());
    // }

    // std::cout << "enter key to exit...";
    // std::cin.get();
    // return 0;

    // if (argc == 2)
    // {
    //     std::cout << "start ..." << args[0] << std::endl;
    //     CppWinApi::Windows::Dialog::Show(args[0]);

    //     return 0;
    // }

    // 创建随机数生成器
    boost::uuids::random_generator generator;
    // 生成一个随机 UUID
    boost::uuids::uuid uuid = generator();
    boost::uuids::to_string(uuid);

    std::cout << boost::uuids::to_string(uuid) << std::endl;

    std::cin.get();

    return 0;

    // std::string command(args[1]);

    // if (command == "startup") // 1. 启动初始化
    // {
    //     CppWinApi::Windows::Dialog::Show("startup");
    //     Startup startup;
    //     auto workProcessId = std::stoi(args[2]);
    //     // 启动守护进程
    //     startup.Run(workProcessId);
    // }
    // else if (command == "launch-daemon-process") // 2. 启动守护进程
    // {
    //     CppWinApi::Windows::Dialog::Show(("launch-daemon-process " + std::to_string(::GetCurrentProcessId())).c_str());
    //     std::string uuid(args[2]);
    //     std::shared_ptr<DeamonProcess> deamonProcess(new DeamonProcess(uuid));

    //     // 启动守护进程守卫
    //     deamonProcess->LaunchGuardProcess();

    //     // 守护工作进程
    //     deamonProcess->ProtectingWorkProcessAsync();

    //     // 守护进程守卫
    //     deamonProcess->ProtectingGuardProcessAsync()
    //         .wait(); // 阻塞进程，防止退出
    //     return 0;
    // }
    // else if (command == "launch-daemon-process-guard") // 3. 守护进程启动进程守卫
    // {
    //     CppWinApi::Windows::Dialog::Show(("launch-daemon-process-guard " + std::to_string(::GetCurrentProcessId())).c_str());

    //     std::string uuid(args[2]);
    //     std::shared_ptr<GuardProcess> guardProcess(new GuardProcess(uuid));

    //     // 保护守护进程
    //     auto future = guardProcess->ProtectingDaemonProcessAsync();
    //     future.wait();
    //     // 打开守护进程守卫
    //     return 0;
    // }
    // else if (command == "launch-daemon-process-from-guard") // 4. 从守卫启动守护进程
    // {
    //     std::string uuid(args[2]);
    //     CppWinApi::Windows::Dialog::Show(("launch-daemon-process-from-guard " + std::to_string(::GetCurrentProcessId())).c_str());

    //     std::shared_ptr<DeamonProcess> deamonProcess(new DeamonProcess(uuid));

    //     // 守护工作进程
    //     deamonProcess->ProtectingWorkProcessAsync();

    //     // 守护进程守卫
    //     deamonProcess->ProtectingGuardProcessAsync().wait();
    //     return 0;
    // }

    return 0;
}
